<!--
 * @Author: 330GRX 617947173@qq.com
 * @Date: 2023-09-27 11:12:31
 * @LastEditors: 330GRX 617947173@qq.com
 * @LastEditTime: 2023-10-02 11:26:23
 * @FilePath: \Realtime-Chat\web\src\views\Register.vue
 * @Description: 
 * 
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved. 
-->
<template>
  <div>
    <div class="FormContainer">
      <form @submit="(e) => handleSubmit(e)">
        <div class="brand">
          <img :src="Logo" alt="" />
          <h1>VueChat</h1>
        </div>
        <input type="text" placeholder="请输入用户名" name="username" v-model="userForm.username" />
        <input type="email" placeholder="请输入邮箱" name="email" v-model="userForm.email" />
        <input type="password" placeholder="请输入密码" name="password" v-model="userForm.password" />
        <input type="password" placeholder="请确认密码" name="confirm" v-model="userForm.confirm" />
        <span class="button">
          <button type="submit">注册</button>
          <button type="reset" @click="(e) => handleReset(e)">重置</button>
        </span>

        <span>已有账号？<a href="/login">去登陆</a></span>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { notification } from 'ant-design-vue'
import { registerRoute } from '@/utils/APIRoutes'
import { useRouter } from 'vue-router'
import Logo from '../assets/logo.svg'
import axios from 'axios'

// 注册信息表单
const userForm = ref({
  username: 'garritsen',
  email: '123@qq.com',
  password: '123456',
  confirm: '123456'
})
// 路由实例对象
const router = useRouter()

/**
 * @description:处理点击注册事件
 * @param {*} e 事件对象
 */
const handleSubmit = async (e) => {
  e.preventDefault()
  if (handleValidation(userForm)) {
    const { username, email, password } = userForm.value
    const { data } = await axios.post(registerRoute, {
      username,
      email,
      password
    })
    if (data.status === false) {
      openNotificationWithIcon('error', data.msg)
    } else {
      localStorage.setItem('chat-app-user', JSON.stringify(data.user))
      router.push('/')
    }
  }
}
/**
 * @description: 校验注册信息表单
 * @param {Object} form 注册信息表单
 */
const handleValidation = (form) => {
  const { username, email, password, confirm } = form.value
  if (password !== confirm) {
    openNotificationWithIcon('error', '前后两次输入密码不同！')
    return false
  } else if (username.trim().length < 3) {
    openNotificationWithIcon('error', '用户名长度应不少于3个字符！')
    return false
  } else if (email.trim() === '') {
    openNotificationWithIcon('error', '邮箱不能为空！')
    return false
  } else if (password.trim().length < 6) {
    openNotificationWithIcon('error', '密码长度应不少于6个字符！')
    return false
  }
  return true
}
/**
 * @description: AntDesign的api，显示弹窗信息
 * @param {string} type 图标，可选值有success、info、warning、error
 * @param {string | VNode | () => VNode} message 通知提醒标题，必选
 */
const openNotificationWithIcon = (type, message) => {
  notification[type]({
    message,
    description: ''
  })
}
/**
 * @description: 处理点击重置事件
 * @param {*} e
 */
const handleReset = (e) => {
  e.preventDefault()
  userForm.value = { username: '', email: '', password: '', confirm: '' }
}
</script>

<style lang="scss" scoped>
.FormContainer {
  height: 100vh;
  width: 100vw;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 1rem;
  align-items: center;
  background-color: #131324;
  .brand {
    display: flex;
    align-items: center;
    gap: 1rem;
    justify-content: center;
    img {
      height: 5rem;
    }
    h1 {
      color: white;
      text-transform: uppercase;
    }
  }
  form {
    display: flex;
    flex-direction: column;
    gap: 2rem;
    background-color: #00000076;
    border-radius: 2rem;
    width: 40vw;
    padding: 3rem 5rem;
    input {
      background-color: transparent;
      padding: 1rem;
      border: 0.1rem solid rgb(56, 72, 93);
      color: white;
      width: 100%;
      font-size: 1rem;
      &:focus {
        border: 0.1rem solid rgb(100, 182, 135);
        outline: none;
      }
    }
    .button {
      display: grid;
      grid-template-columns: 50% 50%;
    }
    button {
      background-color: rgb(56, 72, 93);
      color: white;
      margin: 0 2rem;
      padding: 1rem 2rem;
      border: none;
      font-weight: bold;
      cursor: pointer;
      border-radius: 0.4rem;
      font-size: 1rem;
      @media screen and (min-width: 720px) and (max-width: 1080px) {
        font-size: 0.5rem;
        padding: 0.5rem 1rem;
        margin: 0 0.5rem;
      }
      transition: 0.5s ease-in-out;
      &:hover {
        background-color: rgb(100, 182, 135);
      }
    }
    span {
      color: white;
      text-transform: uppercase;
      text-align: center;
      a {
        color: rgb(100, 182, 135);
        text-decoration: none;
        font-weight: bold;
      }
    }
  }
}
</style>
